package com.atguigu.gulimall.search.thread;

/**
 * @version 1.0
 * @Author yanjingren
 * @Date 2021/6/1
 * @Description com.atguigu.gulimall.search.thread
 */

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 异步编排
 */
public class CompletableFutureTest {
    public static ExecutorService service = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main开始了");

        // 无返回值
//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println(Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//        }, service);

        // 有返回值
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println(Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, service);
//        System.out.println("返回结果为：" + future.get());

        // 完成时回调方法，方法完成后的感知
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println(Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, service).whenComplete((res, exception) -> {  // 结果，异常
//            System.out.println("结果是：" + res + "；异常是：" + exception);
//        }).exceptionally(throwable -> {
//            // 可以感知到异常，同时返回默认值
//            return 10;
//        });

        // 处理结果，方法完成后的处理
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println(Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, service).handle((res, throwable) -> {
//            if (res != null) {
//                return res * 2;
//            }
//            if (throwable != null) {
//                return 0;
//            }
//            return 0;
//        });
//        System.out.println("返回结果为：" + future.get());

        // 线程串行
        // thenRun 不能获取到上一步的执行结果，无返回值
        // thenAcceptAsync 能接受上一步结果，但是无返回值
        // thenApplyAsync 能收受上一步结果，有返回值
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println(Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, service).thenApplyAsync(res -> {
//            System.out.println("任务2启动了..." + res);
//            return "Hello " + res;
//        }, service);
//        System.out.println("返回结果为：" + future.get());

        // 两任务组合 - 都要完成
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("任务1结束：" + i);
            return i;
        }, service);

        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2：" + Thread.currentThread().getId());
            System.out.println("任务2结束");
            return "hello";
        }, service);

        CompletableFuture<String> future = future01.thenCombineAsync(future02, (res1, res2) -> res1 + ":" + res2 + "-> Haha", service);


        System.out.println("main....end....." + future.get());
    }
}
